package com.henghe.frame.common.util;

import com.henghe.frame.common.exception.DecryException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/17 10:47
 */
public class AESUtil {

    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    private AESUtil() {
    }

    public static String encrypt(String src, String key) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(1, makeKey(key), makeIv("4OFGE7llXdTtnjLM"));
            return bytesToHex(cipher.doFinal(src.getBytes())).toLowerCase();
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public static String decrypt(String src, String key) {
        String decrypted = "";
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, makeKey(key), makeIv("4OFGE7llXdTtnjLM"));
            decrypted = new String(cipher.doFinal(hexStringToByteArray(src)));
            return decrypted;
        } catch (Exception var5) {
            throw new DecryException(var5);
        }
    }

    public static String encrypt(String src, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(1, makeKey(key), makeIv(iv));
            return bytesToHex(cipher.doFinal(src.getBytes())).toLowerCase();
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public static String decrypt(String src, String key, String iv) {
        String decrypted = "";

        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, makeKey(key), makeIv(iv));
            decrypted = new String(cipher.doFinal(hexStringToByteArray(src)));
            return decrypted;
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    static AlgorithmParameterSpec makeIv(String iv) {
        try {
            return new IvParameterSpec(iv.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    static Key makeKey(String keyTemp) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.digest(keyTemp.getBytes("UTF-8"));
            return new SecretKeySpec(keyTemp.getBytes(), "AES");
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];

        for (int j = 0; j < bytes.length; ++j) {
            int v = bytes[j] & 255;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 15];
        }

        return new String(hexChars);
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];

        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }

        return data;
    }
}
